package com.lfk.justwetools.Component.CircleGraph;

import ohos.agp.render.Arc;
import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;

import com.lfk.justwetools.ResourceTable;

import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONException;
import ohos.utils.zson.ZSONObject;

/**
 * 环形统计图
 *
 * @author liufengkai
 *         Created by liufengkai on 15/11/6.
 */
public class CircleGraph extends Component implements Component.DrawTask, Component.EstimateSizeListener {
    // 原点
    private int mCircleXY;
    // r
    private float mRadius;
    // 弧线外围的矩形
    private RectFloat mArcRectF;
    // 三个画笔
    private Paint mCirclePaint;
    private Paint mArcPaint;
    private Paint mTextPaint;
    private int mSweepValue = 100;
    private int mStartValue = 0;
    private int mCircleColor;
    private int mArcColor;
    private int ColorSet[] = {0xFFFEDD74, 0xFF82D8EF, 0xFFF76864, 0xFF80BD91, 0xFFFD9FC1, 0xFF};
    private int mArcWidth = 40;
    // 文字
    private String text = null;
    // 文字尺寸
    private float textSize = 40;
    private Context context;
    private boolean jsonFlag = false;
    private ZSONArray Json = null;

    public CircleGraph(Context context) {
        super(context);
        this.context = context;
        initView();
    }


    public CircleGraph(Context context, AttrSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    private void initView() {
        Point screenSize = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(screenSize);
        int length = screenSize.getPointXToInt();
        mCircleXY = length / 2;
        mRadius = (float) (length * 0.5) / 2;
        mArcRectF = new RectFloat(
                (float) (length * 0.1),
                (float) (length * 0.1),
                (float) (length * 0.9),
                (float) (length * 0.9));
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(new Color(context.getColor(ResourceTable.Color_colorPrimaryDark)));

        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setColor(new Color(context.getColor(ResourceTable.Color_colorPrimary)));
        mArcPaint.setStyle(Paint.Style.STROKE_STYLE);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setDither(true);
        mArcPaint.setStrokeWidth(mArcWidth);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize((int)textSize);

        addDrawTask(this);
        setEstimateSizeListener(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 圆形
        canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint);
        // 弧线

        // 弧线的外围矩形/ 起始角度 / 角度 / 是否封闭 / 画笔
        if (jsonFlag && Json != null) {
            int start = mStartValue;
            for (int i = 0; i < Json.size(); i++) {
                try {
                    ZSONObject object = Json.getZSONObject(i);
                    if (object.containsKey("color"))
                        mArcPaint.setColor(new Color(object.getIntValue("color")));
                    canvas.drawArc(mArcRectF, new Arc(start, object.getIntValue("sweep"), false), mArcPaint);
                    start += object.getIntValue("sweep");
                } catch (ZSONException e) {
                    e.printStackTrace();
                }
            }
        } else {
            canvas.drawArc(mArcRectF, new Arc(mStartValue, mSweepValue, false), mArcPaint);
        }
        // 文字
        if (text != null) {
            canvas.drawText(mTextPaint, text,
                    mCircleXY - (text.length() * textSize / 2),
                    mCircleXY + (text.length() * textSize / 2));
        }
    }

    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        setEstimatedSize(measureWidth(widthEstimateConfig),
                measureHeight(heightEstimateConfig));
        return true;
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 500;
        int specMode = EstimateSpec.getMode(heightMeasureSpec);
        int specSize = EstimateSpec.getSize(heightMeasureSpec);
        if (specMode == EstimateSpec.NOT_EXCEED || specMode == EstimateSpec.PRECISE) {
            result = specSize;
        }
        return EstimateSpec.getSizeWithMode(result, EstimateSpec.NOT_EXCEED);
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 500;
        int specMode = EstimateSpec.getMode(widthMeasureSpec);
        int specSize = EstimateSpec.getSize(widthMeasureSpec);
        if (specMode == EstimateSpec.NOT_EXCEED || specMode == EstimateSpec.PRECISE) {
            result = specSize;
        }
        return EstimateSpec.getSizeWithMode(result, EstimateSpec.NOT_EXCEED);
    }

    /**
     * 设定起始角度
     *
     * @param mStartValue
     */
    public void setmStartValue(int mStartValue) {
        this.mStartValue = mStartValue;
        invalidate();
    }

    /**
     * 设定百分比
     *
     * @param mSweepValue
     */
    public void setmSweepValue(int mSweepValue) {
        this.mSweepValue = mSweepValue;
        invalidate();
    }

    /**
     * 弧线颜色
     *
     * @param mArcColor
     */
    public void setmArcColor(int mArcColor) {
        this.mArcColor = mArcColor;
        mArcPaint.setColor(new Color(mArcColor));
        invalidate();
    }

    /**
     * 圆形颜色
     *
     * @param mCircleColor
     */
    public void setmCircleColor(int mCircleColor) {
        this.mCircleColor = mCircleColor;
        mCirclePaint.setColor(new Color(mCircleColor));
        invalidate();
    }

    /**
     * 设定文本
     *
     * @param text
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * 设定圆弧宽度
     *
     * @param mArcWidth
     */
    public void setmArcWidth(int mArcWidth) {
        this.mArcWidth = mArcWidth;
    }

    /**
     * 设定文字尺寸
     *
     * @param textSize
     */
    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public void setJson(String json) {
        try {
            Json = ZSONArray.stringToZSONArray(json);
            jsonFlag = true;
        } catch (ZSONException e) {
            e.printStackTrace();
            jsonFlag = false;
        }
    }
}
